# coding: utf-8

require 'fileutils'
require 'fastools'

default_platform(:ios)
xcode_select(ENV['XCODE_SELECT'])

platform :ios do

  private_lane :build_universal_framework do
    demo_project_path = ENV['DEMO_PROJECT_PATH']
    target_project = ENV['PROJECT_NAME']
    universal_outputfolder = "../#{demo_project_path}/build/Release-universal/"
    iphoneos_outputfolder = "../#{demo_project_path}/build/Release-iphoneos/"
    iphonesimulator_outputfolder = "../#{demo_project_path}/build/Release-iphonesimulator/"

    FileUtils.rm_rf "../#{demo_project_path}/build", :verbose => true
     
    build_framework(sdk: "iphonesimulator", configuration: "Release")
    build_framework(sdk: "iphoneos", configuration: "Release")

    Fastools::Lipo.remove(
      arch_type: "arm64",
      framework: "#{iphonesimulator_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}"
    )
    Fastools::Lipo.info(
        output_framework: "#{iphonesimulator_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}"
    )

    FileUtils.mkdir_p "#{universal_outputfolder}", :verbose => true
    FileUtils.cp_r "#{iphoneos_outputfolder}/#{target_project}", universal_outputfolder, :verbose => true
    FileUtils.cp_r "#{iphonesimulator_outputfolder}/#{target_project}", universal_outputfolder, :verbose => true
    FileUtils.rm "#{universal_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}", :verbose => true

    Fastools::Lipo.create(
        output_framework: "#{universal_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}",
        input_frameworks: [
          "#{iphonesimulator_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}",
          "#{iphoneos_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}"
        ]
    )

    Fastools::Lipo.info(
        output_framework: "#{universal_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}"
    )
  end

  lane :build_frameworks do

    demo_project_path = ENV['DEMO_PROJECT_PATH']
    
    framework_scheme = ENV['POD_SPEC_NAME']
    
    framework_repo_path = ENV['FRAMEWORK_REPO_PATH'] 

    iphoneos_outputfolder = "../#{demo_project_path}/build/Release-iphoneos/"
    iphonesimulator_outputfolder = "../#{demo_project_path}/build/Release-iphonesimulator/"

    FileUtils.rm_rf "../#{demo_project_path}/build", :verbose => true
     
    build_framework(sdk: "iphonesimulator", framework_scheme: target_project, configuration: "Release")
    build_framework(sdk: "iphoneos", framework_scheme: target_project, configuration: "Release")

    FileUtils.mkdir_p "#{framework_repo_path}/iphonesimulator/#{target_project}.framework"
    FileUtils.cp_r "#{iphonesimulator_outputfolder}/#{target_project}/#{target_project}.framework", "#{framework_repo_path}/iphonesimulator", :verbose => true

    FileUtils.mkdir_p "#{framework_repo_path}/iphoneos/#{target_project}.framework"
    FileUtils.cp_r "#{iphoneos_outputfolder}/#{target_project}/#{target_project}.framework", "#{framework_repo_path}/iphoneos", :verbose => true

    Fastools::Lipo.info(
        output_framework: "#{iphoneos_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}"
    )
    Fastools::Lipo.info(
        output_framework: "#{iphonesimulator_outputfolder}/#{target_project}/#{target_project}.framework/#{target_project}"
    )
  end

  private_lane :build_framework do |options|
    skd = options[:sdk]
    configuration = options[:configuration]
    framework_scheme = options[:framework_scheme]

    pods_project_path = "#{ENV['DEMO_PROJECT_PATH']}/Pods/Pods.xcodeproj"

    gym(
      scheme: framework_scheme,
      project: pods_project_path,
      configuration: configuration,
      include_bitcode: true,
      clean: false,
      sdk: skd,
      skip_build_archive: false,
      skip_archive: true,
      xcargs: { 
        :DEPLOYMENT_POSTPROCESSING => "YES",
        :OTHER_CFLAGS => "-fembed-bitcode",
        :BITCODE_GENERATION_MODE => "bitcode"
      }
    )

  end

  private_lane :deliver_framework do
    target_project = ENV['PROJECT_NAME']

    demo_project_path = ENV['DEMO_PROJECT_PATH']
    target_project = ENV['PROJECT_NAME']
    framework_repo_path = ENV['FRAMEWORK_REPO_PATH'] 

    universal_outputfolder = "../#{demo_project_path}/build/Release-universal/"

    FileUtils.cp_r "#{universal_outputfolder}/#{target_project}/#{target_project}.framework", framework_repo_path, :verbose => true

  end
end





